package com.aura.springbook.service.impl;

import com.aura.springbook.mapper.BookMapper;
import com.aura.springbook.model.Book;
import com.aura.springbook.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 图书服务实现类
 * <p>
 * 继承MyBatis-Plus的ServiceImpl，实现BookService接口
 * 提供图书相关的业务逻辑实现
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    private static final Logger logger = LoggerFactory.getLogger(BookServiceImpl.class);

    @Autowired
    private BookMapper bookMapper;

    /**
     * 分页查询所有图书
     *
     * @param current 当前页
     * @param size    每页大小
     * @return 图书分页数据
     */
    @Override
    public IPage<Book> getBooksByPage(int current, int size) {
        logger.debug("开始分页查询所有图书, 当前页: {}, 每页大小: {}", current, size);
        Page<Book> page = new Page<>(current, size);
        IPage<Book> result = bookMapper.selectPage(page, null);
        logger.debug("分页查询所有图书完成, 总记录数: {}", result.getTotal());
        return result;
    }

    /**
     * 根据分类ID分页查询图书
     *
     * @param categoryId 分类ID
     * @param current    当前页
     * @param size       每页大小
     * @return 图书分页数据
     */
    @Override
    public IPage<Book> getBooksByCategory(Long categoryId, int current, int size) {
        logger.debug("开始按分类ID分页查询图书, 分类ID: {}, 当前页: {}, 每页大小: {}", categoryId, current, size);
        Page<Book> page = new Page<>(current, size);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        IPage<Book> result = bookMapper.selectPage(page, queryWrapper);
        logger.debug("按分类ID分页查询图书完成, 分类ID: {}, 总记录数: {}", categoryId, result.getTotal());
        return result;
    }

    /**
     * 根据关键词搜索图书
     *
     * @param keyword 关键词
     * @param current 当前页
     * @param size    每页大小
     * @return 图书分页数据
     */
    @Override
    public IPage<Book> searchBooks(String keyword, int current, int size) {
        logger.debug("开始搜索图书, 关键词: {}, 当前页: {}, 每页大小: {}", keyword, current, size);
        Page<Book> page = new Page<>(current, size);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword).or().like("author", keyword);
        IPage<Book> result = bookMapper.selectPage(page, queryWrapper);
        logger.debug("搜索图书完成, 关键词: {}, 总记录数: {}", keyword, result.getTotal());
        return result;
    }

    /**
     * 根据ID获取图书详情
     *
     * @param id 图书ID
     * @return 图书详情
     */
    @Override
    public Book getBookDetail(Long id) {
        logger.debug("开始获取图书详情, 图书ID: {}", id);
        Book book = bookMapper.selectById(id);
        if (book != null) {
            logger.debug("获取图书详情成功, 图书ID: {}, 图书标题: {}", id, book.getTitle());
        } else {
            logger.warn("获取图书详情失败, 图书ID: {} 不存在", id);
        }
        return book;
    }
}